<template>
  <div class="y-conter">
    <div class="y-nav-top">
      <h3 class="y-bold">门禁设备</h3>
      <!-- 搜索输入框部分 -->
      <div class="l-input">
        <div class="l-input-left">
          <el-form
            :inline="true"
            ref="ruleForm"
            :model="ruleForm"
            class="demo-form-inline"
          >
            <el-form-item label="公司名称:">
              <el-select v-model="ruleForm.equipment_name" placeholder="请选择">
                <el-option
                  v-for="item in tableData"
                  :key="item.index"
                  :label="item.equipment_name"
                  :value="item.equipment_name"
                ></el-option>
              </el-select>
            </el-form-item>
            <!-- 按钮搜和索部分 -->
            <el-form-item label="ip地址：">
              <el-input
                v-model="ruleForm.equipment_ip"
                placeholder="请输入"
                class="padding"
              ></el-input>
              <!-- 按钮搜和索部分 -->
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="search('ruleForm')"
                >搜索</el-button
              >
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div class="y-btn">
          <el-button class="y-btn1-right" type="primary" @click="Newadditions()"
            >新增</el-button
          >
        </div>
      </div>
      <!-- 表格部分 -->
      <div class="y-table">
        <el-table
          :border="true"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          ref="multipleTable"
          :data="currList"
          highlight-current-row
          tooltip-effect="dark"
          style="width: 98%, text-align: center "
          @selection-change="handleSelectionChange"
        >
          <!-- 复选框 -->
          <el-table-column
            align="center"
            type="index"
            width="100"
            :index="indexFn"
            label="序号"
          ></el-table-column>
          <el-table-column
            label="设备名称"
            prop="equipment_name"
            width="150"
            hight="30px"
            align="center"
          ></el-table-column>
          <el-table-column
            label="设备编号"
            prop="equipment_code"
            width="120"
            align="center"
          ></el-table-column>
          <el-table-column
            label="品牌名称"
              width="100"
            prop="equipment_brand"
            align="center"
          ></el-table-column>
          <el-table-column
            label="ip地址"
            prop="equipment_ip"
            width="160"
            align="center"
          ></el-table-column>
          <el-table-column
            label="安装时间"
            prop="equipment_install"
            width="160"
            align="center"
          ></el-table-column>
          <el-table-column
            label="安装地址"
            prop="equipment_addr"
            width="100"
            align="center"
          ></el-table-column>
          <el-table-column
            label="在线状态"
            prop="equipment_status"
            width="100"
            align="center"
          >
            <template slot-scope="scope">
              <span
                class="el-icon-success"
                style="color: green"
                v-if="scope.row.equipment_status == '0'"
              ></span>
              <span class="el-icon-remove" style="color: red" v-else></span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                type="text"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
              <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <div class="y-fenye">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="tableData.length"
            :page-size="pageSize"
            @current-change="changePage"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import http from "../api";
// import axios from "axios";
export default {
  data() {
    return {
      // remind:"",
      //搜索输入框
      ruleForm: {
        equipment_name: "", //公司名称
        equipment_ip: "", //ip地址
       equipment_type_id: "1",
      },
      //多选表格的复选框
      multipleSelection: [],
      // 分页
      tableData: [], //总数居
      currPage: 1, //当前页
      pageSize: 5, //每页数据量
      currList: [], //当前展示的数据数组
    };
  },
  computed: {},
  //------- 跳转新增------
  methods: {
    // 新增
    Newadditions() {
      this.$router.push({
        name: "门禁设备管理",
      });
    },
    //表格编辑按钮
    handleEdit(index, row) {
      console.log(index, row);
      this.$router.push({
        name: "门禁设备编辑",
        params: row,
      });
    },
    // -------分页------
    setCurrList(index) {
      this.currList = this.tableData.slice(
        (index - 1) * this.pageSize,
        index * this.pageSize
      );
      // console.log('当前页',index);
    },
    //改变当前页
    changePage(val) {
      this.currPage = val;
      this.setCurrList(val);

      console.log(val);
    },
    // 表格序号
    indexFn(index) {
      index = index + 1 + (this.currPage - 1) * this.pageSize;
      return index;
    },
    //重置按钮
    resetForm(formName) {
      this[formName] = {};
      this.$refs[formName].resetFields();
    },

    // 表格删除按钮
    // 表格删除按钮
    handleDelete(index, row) {
      console.log(row);
      http.del(row).then((r) => {
        http
          .system({
            equipment_type_id: "1",
          })
          .then((r) => {
            console.log(r);
            this.tableData = r.data;
            console.log(r.data);

            // console.log(this.tableData);
            this.setCurrList(1);
          });

        console.log(r);
        console.log("单个删除", r.data);
      });
    },
    //选中复选框获取下标
    handleSelectionChange(val) {
      // this.multipleSelection = val;
      // let ids = [];
      // this.multipleSelection.map((item) => {
      //   ids.push(item.key);
      // });
      // this.selectedIDs = ids;
      // console.log("你点击的是", this.selectedIDs);
    },

    //搜索输入
    search(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          http.system(this.ruleForm).then((r) => {
            console.log("返回的数据", r);
            this.tableData = r.data;
            this.setCurrList(1);
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

   
  },
  //获取数据
  mounted() {
    http
      .system({
        equipment_type_id: "1",
      })
      .then((r) => {
        console.log(r);
        this.tableData = r.data;
        console.log(r.data);
        this.setCurrList(1);
      });
  },
};
</script>
<style scoped>
.padding >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px !important;
}
.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
  padding-left: 5px;
  /* margin-left: 5px; */
}

/* 大容器 */
.y-conter {
  width: 100%;
  height: 640px;
  position: relative;
  overflow: hidden;
  overflow-y: scroll;
}
.y-nav-top {
  width: 100%;
  height: 640px;
  padding-top: 20px;
  margin-top: 20px;
  background-color: white;
}
/* 表格 */
.y-table {
  width: 100%;
  height: 400px;
  margin-top: 10px;
  margin-left: 10px;
}
.y-table {
  margin-left: 20px;
  width: 98%;
  height: 600px;
}
.l-input {
  padding-left: 20px;
  display: flex;
  justify-content: space-between;
}
/* 右侧新增导出按钮 */
.y-btn {
  margin-top: 10px;
  margin-right: 10px;
}
/* 所有按钮 */
.el-button {
  height: 30px;
  line-height: 0px;
}
/* 标题字体 */
.y-bold {
  font-weight: bold;
  padding-left: 20px;
}

.el-select >>> .el-input__inner {
  width: 178px;
  height: 30px;
  /* margin-left: 5px; */
}
.el-input >>> .el-input__inner {
  width: 178px;
  height: 30px;
  /* margin-left: 5px; */
}
.el-range-editor {
  width: 160px;
  height: 30px;
  padding-left: 10px;
  margin-left: 5px;
}
.el-input >>> .el-range__icon {
  line-height: 0px;
  padding-left: 10px;
}
.el-range-editor >>> .el-input__inner {
  width: 200px;
  height: 30px;
  margin-left: 5px;
  margin-top: 10px;
}
.y-fenye {
  float: right;
  margin-top: 20px;
}

</style>
